package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.baidubce.qianfan.util.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.page.TableDataInfo;

import com.zzyl.nursing.Task.Content;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDataPageReqDto;
import com.zzyl.nursing.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备数据Service业务层处理
 * 
 * @author alexis
 * @date 2025-01-12
 */
@Service
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 查询设备数据
     *
     * @param id 设备数据主键
     * @return 设备数据
     */
    @Override
    public DeviceData selectDeviceDataById(Long id) {
        return deviceDataMapper.selectById(id);
    }


    @Override
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData) {
    return deviceMapper.selectDeviceDataList(deviceData);

    }


//    private TableDataInfo getTableDataInfo(Page<DeviceData> page) {
//        TableDataInfo tableData = new TableDataInfo();
//        tableData.setCode(HttpStatus.SUCCESS);
//        tableData.setMsg("查询成功");
//        tableData.setRows(page.getRecords());
//        tableData.setTotal(page.getTotal());
//        return tableData;
//    }


    /**
     * 查询设备数据列表
     *
     * @param deviceData 设备数据
     * @return 设备数据
     */
//    @Override
//    public List<DeviceData> selectDeviceDataList(DeviceData deviceData)
//    {
//        return deviceDataMapper.selectDeviceDataList(deviceData);
//    }

    /**
     * 新增设备数据
     *
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int insertDeviceData(DeviceData deviceData) {
        return deviceDataMapper.insert(deviceData);
    }

    /**
     * 修改设备数据
     *
     * @param deviceData 设备数据
     * @return 结果
     */
    @Override
    public int updateDeviceData(DeviceData deviceData) {
        return deviceDataMapper.updateById(deviceData);
    }

    /**
     * 批量删除设备数据
     *
     * @param ids 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataByIds(Long[] ids) {
        return deviceDataMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备数据信息
     *
     * @param id 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteDeviceDataById(Long id) {
        return deviceDataMapper.deleteById(id);
    }

    /**
     * 批量新增设备数据
     *
     * @param content
     */
    @Override
    @Transactional( rollbackFor = Exception.class)
    public void batchInsertDeviceData(Content content) {
            // 查询设备，如果设备为空，则直接中断
            Device device = deviceMapper.selectOne(Wrappers.<Device>lambdaQuery().eq(Device::getIotId, content.getIotId()));
            if (device == null) {
                log.error("设备不存在,iotId: {}");
                return;
            }
            // 批量构建设备数据
        List<DeviceData> list = new ArrayList<>();
        content.getItems().forEach((key,item) -> {
            DeviceData deviceData = DeviceData.builder()
                    .deviceName(device.getDeviceName())
                    .iotId(device.getIotId())
                    .nickname(device.getNickname())
                    .productKey(device.getProductKey())
                    .productName(device.getProductName())
                    .functionId(key)
                    .accessLocation(device.getRemark())
                    .locationType(device.getLocationType())
                    .physicalLocationType(device.getPhysicalLocationType())
                    .deviceDescription(device.getDeviceDescription())
                    .dataValue(item.getValue()+"")
                    .alarmTime(LocalDateTimeUtil.of(item.getTime()))
                    .build();
            list.add(deviceData);
        });
            // 批量保存
        saveBatch(list);
        redisTemplate.opsForHash().put(CacheConstants.IOT_DEVICE_LAST_DATA, content.getIotId(), list);
    }
}


